package com.module.base.base.adapter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.OvershootInterpolator;

import androidx.annotation.CallSuper;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;

import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Description :
 *
 * @author bsnl_yanxuwen
 * @date 2019/12/1615:13
 */
public abstract class BaseNewViewHolder<T> extends BaseViewHolder implements LifecycleOwner, LifecycleObserver {

    public Context context;

    private BaseNewAdapter adapter;

    private List<View> showAminView;

    public boolean isViewRecycled;

    protected LifecycleRegistry lifecycleRegistry;

    private T t;

    public AnimatorSet getShowAnimators(View view) {
        AnimatorSet animatorSet = new AnimatorSet();
        ObjectAnimator objectAnimator01 = ObjectAnimator.ofFloat(view, "alpha", 0, 1f);
        animatorSet.play(objectAnimator01);
        animatorSet.setDuration(1000);
        animatorSet.setInterpolator(new OvershootInterpolator(0.5f));
        return animatorSet;
    }

    public BaseNewViewHolder(ViewGroup parent, @LayoutRes int idRes) {
        super(LayoutInflater.from(parent.getContext()).inflate(idRes, parent, false));
//        ButterKnife.bind(this, itemView);
    }

    public BaseNewViewHolder(View parent) {
        super(parent);
        lifecycleRegistry = new LifecycleRegistry(this);
//        ButterKnife.bind(this, itemView);
    }


    protected abstract void convert(@NonNull T bean, int position);

    @CallSuper
    public void initView() {
    }

    public T getData() {
        return t;
    }

    public void setData(T t) {
        this.t = t;
        //本来是要放在convert函数里面的，但是由于convert是抽象，setData跟convert是一起的，所以就放在这边
        if (lifecycleRegistry != null) {
            final Lifecycle.Event event = Lifecycle.Event.upFrom(lifecycleRegistry.getCurrentState());
            if (event == null) {
                lifecycleRegistry = new LifecycleRegistry(this);
            }
            lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
        }
    }

    public BaseViewHolder setAdapter(BaseDiscardAdapter adapter) {
        if (adapter instanceof BaseNewAdapter) {
            this.adapter = (BaseNewAdapter) adapter;
        }
        this.initView();
        return this;
    }

    public BaseNewAdapter getAdapter() {
        return adapter;
    }

    /**
     * 添加需要显示动画的View
     */
    public void addShowAminView(View... views) {
        if (views == null) {
            return;
        }
        if (showAminView == null) {
            showAminView = new ArrayList();
        }
        for (int i = 0; i < views.length; i++) {
            showAminView.add(views[i]);
        }
    }

    /**
     * 添加需要显示动画的View
     */
    public void addShowAminIdRes(int... idRes) {
        if (idRes == null) {
            return;
        }
        View[] views = new View[idRes.length];
        for (int i = 0; i < idRes.length; i++) {
            views[i] = itemView.findViewById(idRes[i]);
        }
        addShowAminView(views);
    }


    @Override
    public BaseViewHolder addOnClickListener(int viewId) {
        final View view = getView(viewId);
        if (view == null) {
            return this;
        }
        if (!view.isClickable()) {
            view.setClickable(true);
        }
        if (adapter.mOnItemOtherClickListener != null) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    adapter.setOnItemOtherClick(view, getCurPosition());
                }
            });
        }

        return this;
    }

    @Override
    public BaseViewHolder addOnLongClickListener(@IdRes final int viewId) {
        try {
            super.addOnLongClickListener(viewId);
        } catch (Exception e) {
        }
        final View view = getView(viewId);
        if (view == null) {
            return this;
        }
        if (!view.isClickable()) {
            view.setClickable(true);
        }
        if (adapter.mOnItemOtherLongClickListener != null) {
            view.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return adapter.setOnItemOtherLongClick(view, getCurPosition());
                }
            });
        }
        return this;
    }

    /**
     * 当前的position
     */
    public int getCurPosition() {
        if (getLayoutPosition() >= adapter.getHeaderLayoutCount()) {
            return getLayoutPosition() - adapter.getHeaderLayoutCount();
        }
        return 0;
    }

    /**
     * 当前的position , 包含了头部
     */
    public int getRealPosition() {
        return getLayoutPosition();
    }

    /**
     * 停止滚动的时候，第一个BaseViewHolder如果是当前的，则调用
     */
    @CallSuper
    public void onViewIdleFirstWindow() {
    }

    /**
     * 进入监听t
     */
    @CallSuper
    public void onViewAttachedToWindow() {
        getAdapter().onViewIdleFirstWindow(this, true);
        if (showAminView != null && !showAminView.isEmpty() && getAdapter() != null && getAdapter().isShowAmin(getCurPosition())) {
            if (getAdapter().isRefresh() && getAdapter().getFirstVisibleItem() < 2 && !getAdapter().isFirstRefresh()) {
                //如果是刷新，且第一个可见视图小于2，且map_animate不为空，则代表之前有数据，不执行动画，避免闪烁
            } else {
                getAdapter().setShowAmin(getCurPosition(), true);
                for (View views : showAminView) {
                    getShowAnimators(views).start();
                }
            }
        }
        if (lifecycleRegistry != null) {
            lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
        }
    }

    /**
     * 回收监听
     */
    @CallSuper
    public void onViewRecycled() {
        isViewRecycled = true;
        if (showAminView != null && !showAminView.isEmpty() && getAdapter() != null) {
            getAdapter().setShowAmin(getCurPosition(), false);
        }
        if (lifecycleRegistry != null && lifecycleRegistry.getCurrentState().isAtLeast(Lifecycle.State.CREATED)) {
            lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
        }
    }

    /**
     * 离开监听
     */
    @CallSuper
    public void onViewDetachedFromWindow() {
        if (lifecycleRegistry != null) {
            lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
        }
    }

    @NonNull
    @Override
    public Lifecycle getLifecycle() {
        return lifecycleRegistry;
    }
}
